package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.utils.WmThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

@Service
@Transactional
@Slf4j
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper , WmMaterial> implements WmMaterialService{

    @Autowired
    private FileStorageService fileStorageService ;

    @Autowired
    private WmMaterialService wmMaterialService ;

    public ResponseResult uploadImageFile(MultipartFile multipartFile){

        //导入minio-file-starter 并在nacos为他配置参数
        //文件上传
        //参数检查
        if (multipartFile == null || multipartFile.getSize() ==0 ){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //文件名准备
        String newName = UUID.randomUUID().toString().replace("-", "");
        String extendName = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));

        String fileUrl = null ;
        //上传文件
        try {
            InputStream inputStream = multipartFile.getInputStream();
            //注意这里不同的 文件调用不同的上传方法，不然浏览器打不开
             fileUrl = fileStorageService.uploadImgFile("", newName+extendName, inputStream);
            log.info("文件上传成功，文件id：{}",fileUrl);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("FileStorageServices上传文件失败");
        }

        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(WmThreadLocalUtil.get().getId());
        wmMaterial.setType((short)0);
        wmMaterial.setUrl(fileUrl);
        wmMaterial.setCreatedTime(new Date());
        //文件轮径存储本地
        save(wmMaterial);

        return ResponseResult.okResult("文件上传成功");

    }

    /**
     * 如果要使用分页查询一要 注入（一般是在启动类注入） 分页拦截器
     * @param wmMaterialDto
     * @return
     */
    @Override
    public ResponseResult pageList(WmMaterialDto wmMaterialDto) {
        //参数检查，封装在wmMaterialDto 父类里面
        wmMaterialDto.checkParam();

        //分页查询
        IPage page = new Page(wmMaterialDto.getPage() , wmMaterialDto.getSize());
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
        //是否收藏
        if ((wmMaterialDto.getIsCollection() != null ) && (wmMaterialDto.getIsCollection() ==1 )){
            queryWrapper.eq(WmMaterial::getIsCollection ,  wmMaterialDto.getIsCollection());
        }
        //按照用户查询
        queryWrapper.eq(WmMaterial::getUserId , WmThreadLocalUtil.get().getId());
        //时间倒序查询
        queryWrapper.orderByDesc(WmMaterial::getCreatedTime);
        //开始分页查
        page = page(page, queryWrapper);

        PageResponseResult pageResponseResult = new PageResponseResult(wmMaterialDto.getPage() ,  wmMaterialDto.getSize() ,(int) page.getTotal());
        pageResponseResult.setData(page.getRecords());

        //结果返回
        return pageResponseResult;
    }
}
